package com.hawthorn.redis.service.menu.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysMenuDTO;
import com.hawthorn.entity.po.SysMenuPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.redis.repository.MenuCacheMapper;
import com.hawthorn.redis.service.menu.MenuCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>todo 加载菜单缓存服务实现类</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/8/31 10:14
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class MenuCacheServiceImpl implements MenuCacheService
{
  @Autowired
  RedisMyClient redisMyClient;

  @Resource
  private MenuCacheMapper menuCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {
    List<SysMenuDTO> menuCache = menuCacheMapper.selectMenus();
    String key;
    String parentKey;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENU_PREFIX, "*"));
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, "*"));

    for (SysMenuDTO menuCacheDTO : menuCache)
    {
      // 设置key:menu缓存
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENU_PREFIX, menuCacheDTO.getId());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ID, String.valueOf(menuCacheDTO.getId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_CODE, menuCacheDTO.getCode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TITLE, menuCacheDTO.getTitle());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TITLEEN, menuCacheDTO.getTitleEn());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_PARENTID, String.valueOf(menuCacheDTO.getParentId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_URL, menuCacheDTO.getUrl());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_COMPONENT, menuCacheDTO.getComponent());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_COMPONENTNAME, menuCacheDTO.getComponentName());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ICON, menuCacheDTO.getIcon());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_LAYERSNUM, menuCacheDTO.getLayersNum());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_PATH, menuCacheDTO.getPath());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_EXTERNALS, menuCacheDTO.getExternal());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ORDERNUM, menuCacheDTO.getOrderNum());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_CACHE, menuCacheDTO.getCache());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_PERMS, menuCacheDTO.getPerms());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TYPE, String.valueOf(menuCacheDTO.getType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_HIDDEN, menuCacheDTO.getHidden());

      // 设置key:parent缓存
      parentKey = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, menuCacheDTO.getParentId());
      if (redisMyClient.hasKey(parentKey))
      {
        String v = redisMyClient.get(parentKey);
        v += "," + menuCacheDTO.getId();
        redisMyClient.set(parentKey, v);
      } else
      {
        redisMyClient.set(parentKey, String.valueOf(menuCacheDTO.getId()));
      }
    }

    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLEMENU_PREFIX, "*"));

    log.info("====== load menudata to redis success ======");
  }

  /**
   * <p>todo 批量，新增or修改父级菜单</p>
   * @param menus
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/21 11:44
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/21 guohao v1.0.1 init
   */
  @Override
  public void CUBatchParentMenu(List<SysMenuPO> menus)
  {
    for (SysMenuPO menu : menus)
    {
      redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, menu.getParentId()));
      // 设置key:parent缓存
      String parentKey = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, menu.getParentId());
      if (redisMyClient.hasKey(parentKey))
      {
        String v = redisMyClient.get(parentKey);
        v += "," + menu.getId();
        redisMyClient.set(parentKey, v);
      } else
      {
        redisMyClient.set(parentKey, String.valueOf(menu.getId()));
      }
    }
    log.info("====== load menudata to redis success ======");
  }

  /**
   * <p>todo 批量，新增or修改子级菜单</p>
   * @param menus
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/21 13:54
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/21 guohao v1.0.1 init
   */
  @Override
  public void CUBatchMenu(List<SysMenuPO> menus)
  {
    for (SysMenuPO menu : menus)
    {
      redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, "*"));
      // 设置key:menu缓存
      String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENU_PREFIX, menu.getId());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ID, String.valueOf(menu.getId()));
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_CODE, menu.getCode());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TITLE, menu.getTitle());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TITLEEN, menu.getTitleEn());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_PARENTID, String.valueOf(menu.getParentId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_URL, menu.getUrl());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_COMPONENT, menu.getComponent());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_COMPONENTNAME, menu.getComponentName());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ICON, menu.getIcon());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_LAYERSNUM, menu.getLayersNum());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_PATH, menu.getPath());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_EXTERNALS, menu.getExternal());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_ORDERNUM, menu.getExternal());
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_CACHE, menu.getCache());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_TYPE, String.valueOf(menu.getType()));
      // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_MENU_HIDDEN, menu.getHidden());
    }
  }

  /**
   * <p>todo 父级菜单删除</p>
   * @param menus
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/21 14:21
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/21 guohao v1.0.1 init
   */
  @Override
  public void removeBatchParentMenu(List<SysMenuPO> menus)
  {
    for (SysMenuPO menu : menus)
    {
      redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENUTREE_PREFIX, menu.getParentId()));
    }
    log.info("====== load menudata to redis success ======");
  }

  /**
   * <p>todo 子级菜单批量删除</p>
   * @param menus
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/21 11:44
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/21 guohao v1.0.1 init
   */
  @Override
  public void removeBatchMenu(List<SysMenuPO> menus)
  {
    for (SysMenuPO menu : menus)
    {
      redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_MENU_PREFIX, menu.getId()));
    }
    log.info("====== load menudata to redis success ======");
  }
}
